package com.buyantech.smweb.controller;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javacommon.base.BaseSpringController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;

import cn.org.rapid_framework.page.Page;

import com.buyantech.smweb.excel.ExcelAVieData;
import com.buyantech.smweb.model.Black;
import com.buyantech.smweb.model.CreditsExchange;
import com.buyantech.smweb.model.User;
import com.buyantech.smweb.service.BlackManager;
import com.buyantech.smweb.service.CreditsExchangeManager;
import com.buyantech.smweb.service.UserManager;
import com.buyantech.smweb.vo.CreditsExchangeDto;
import com.buyantech.smweb.vo.query.BlackQuery;
import com.buyantech.smweb.vo.query.CreditsExchangeQuery;
import com.google.gson.Gson;

/**
 * 积分兑换记录
 * 
 * @version 1.0
 * @date 2012-05-10
 */
@Controller
public class BlackController extends BaseSpringController {
	private BlackManager blackManager;
	private UserManager userManager;

	private final String LIST_ACTION = "redirect:/pages/User/list.do";

	public void setBlackManager(
			BlackManager blackManager) {
		this.blackManager = blackManager;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	public void init(ModelMap model) {
		model.put("now", new java.sql.Timestamp(System.currentTimeMillis()));
	}

	/**
	 * 执行搜索
	 **/
	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response, BlackQuery query) {

		Map<String, Object> modelMap = new HashMap<String, Object>();
		query.setPageNumber(Integer.parseInt(request.getParameter("pageIndex")) + 1);
		query.setPageSize(Integer.parseInt(request.getParameter("pageSize")));
		String sortfile = request.getParameter("sortField");
		if (sortfile != null && !sortfile.equals(""))
			query.setSortColumns(request.getParameter("sortField") + " "
					+ request.getParameter("sortOrder"));
		else
			query.setSortColumns(" crdate desc");
		Page page = this.blackManager.searchPage(query);
		List<Black> list = page.getResult();
		for (Black black : list) {
			if(black.getCrdate()!=null)
			black.setCrdate(black.getCrdate().substring(2));
		}
		modelMap.put("data", list);
		modelMap.put("total", page.getTotalCount());
		JSONObject obj = new JSONObject(modelMap);
		ajaxPack(obj.toString(), request, response);
		return null;
	}

//	/**
//	 * 黑名单
//	 **/
//	public ModelAndView blacklist(HttpServletRequest request,
//			HttpServletResponse response, CreditsExchangeQuery query) {
//
//		Map<String, Object> modelMap = new HashMap<String, Object>();
//		query.setPageNumber(Integer.parseInt(request.getParameter("pageIndex")) + 1);
//		query.setPageSize(Integer.parseInt(request.getParameter("pageSize")));
//		String sortfile = request.getParameter("sortField");
//		if (sortfile != null && !sortfile.equals(""))
//			query.setSortColumns(request.getParameter("sortField") + " "
//					+ request.getParameter("sortOrder"));
//		else
//			query.setSortColumns(" crdate desc");
//		Page page = this.creditsExchangeManager.searchPage1(query);
//		List<String> list = page.getResult();
//		List<CreditsExchange> mList = new ArrayList<CreditsExchange>();
//		for (String str : list) {
//			CreditsExchange mCreditsExchange = new CreditsExchange();
//			mCreditsExchange.setAccount(str);
//			mList.add(mCreditsExchange);
//		}
//		modelMap.put("data", mList);
//		modelMap.put("total", page.getTotalCount());
//		JSONObject obj = new JSONObject(modelMap);
//		ajaxPack(obj.toString(), request, response);
//		return null;
//	}
//
//	/**
//	 * 执行export
//	 **/
//	public ModelAndView exportData(HttpServletRequest request,
//			HttpServletResponse response, CreditsExchangeQuery query) {
//		ModelAndView result = new ModelAndView(new ExcelAVieData());
//		String typeStr = "";
//		try {
//			typeStr = new String(request.getParameter("typeStr").getBytes(
//					"ISO-8859-1"), "utf-8");
//		} catch (UnsupportedEncodingException e) {
//		}
//		if (StringUtils.isNotEmpty(typeStr)) {
//			query.setTypeStr(typeStr);
//		}
//		String type = "";
//		List<String> headers = new ArrayList<String>();
//		headers.add("用户ID");
//		headers.add("用户昵称");
//		headers.add("时间");
//		headers.add("类型");
//		headers.add("积分");
//		headers.add("金额");
//		headers.add("兑换账户");
//		headers.add("状态");
//		headers.add("操作时间");
//		headers.add("IP");
//		query.setPageSize(10000);
//		Page page = this.creditsExchangeManager.searchPage(query);
//
//		result.addObject("headers", headers);
//		result.addObject("type", "1");
//		result.addObject("content", page.getResult());
//		response.setContentType("application/vnd.ms-excel");
//		response.setHeader("Content-Desposition",
//				"attachment; filename=demo.xls");
//		return result;
//	}
//
//	/**
//	 * 根据id获取用户对象
//	 **/
//	public ModelAndView update(HttpServletRequest request,
//			HttpServletResponse response, CreditsExchange creditsExchange)
//			throws Exception {
//		SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		try {
//			String id = request.getParameter("id");
//			String type = request.getParameter("type");
//			String[] ids = id.split(",");
//			for (int i = 0; i < ids.length; i++) {
//				creditsExchange = creditsExchangeManager.getById(ids[i]);
//				if (creditsExchange.getState().equals("1")) {
//					List<User> userlist = userManager
//							.getHql(" from User where userid='"
//									+ creditsExchange.getUserid() + "'");
//					User user = userlist.get(0);
//					if (type.equals("0")) {
//						creditsExchange.setState("2");
//					} else {
//						user.setHuIntegral(user.getHuIntegral()
//								- creditsExchange.getIntegral());
//						userManager.update(user);
//						creditsExchange.setState("3");
//					}
//					creditsExchange.setOperTime(dateFm
//							.format(new java.util.Date()));
//					creditsExchangeManager.update(creditsExchange);
//				}
//			}
//		} catch (Exception e) {
//			ajaxPack("{state:false}", request, response);
//			return null;
//		}
//		ajaxPack("{state:true}", request, response);
//
//		return null;
//	}
//
//	/**
//	 * 更新备注
//	 **/
//	public ModelAndView updateRemark(HttpServletRequest request,
//			HttpServletResponse response, CreditsExchange creditsExchange)
//			throws Exception {
//		SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		try {
//			String id = request.getParameter("id");
//			String json = request.getParameter("submitData");
//			Gson gson = new Gson();
//			Map<String, String> map = new HashMap<String, String>();
//			map = gson.fromJson(json, Map.class);
//			String remark = (String) map.get("remark");
//			String[] ids = id.split(",");
//			for (int i = 0; i < ids.length; i++) {
//			creditsExchange = creditsExchangeManager.getById(ids[i]);
//			if (creditsExchange.getState().equals("1")) {
//				List<User> userlist = userManager
//						.getHql(" from User where userid='"
//								+ creditsExchange.getUserid() + "'");
//				User user = userlist.get(0);
//				user.setHuIntegral(user.getHuIntegral()
//						- creditsExchange.getIntegral());
//				userManager.update(user);
//				creditsExchange.setState("3");
//				creditsExchange.setRemark(remark);
//				creditsExchange
//						.setOperTime(dateFm.format(new java.util.Date()));
//				creditsExchangeManager.update(creditsExchange);
//			}
//			}
//		} catch (Exception e) {
//			ajaxPack("{state:false}", request, response);
//			return null;
//		}
//		ajaxPack("{state:true}", request, response);
//
//		return null;
//	}
//	
//	/**
//	 * 更新黑名单
//	 **/
//	public ModelAndView updateIsBlack(HttpServletRequest request,
//			HttpServletResponse response, CreditsExchange creditsExchange)
//			throws Exception {
////		SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		try {
////			String id = request.getParameter("id");
////			String json = request.getParameter("submitData");
////			Gson gson = new Gson();
////			Map<String, String> map = new HashMap<String, String>();
////			map = gson.fromJson(json, Map.class);
////			String remark = (String) map.get("remark");
////			
////			creditsExchange = creditsExchangeManager.getById(id);
//			String account=request.getParameter("account");
//			String str="from credits_exchange where account='"+account+"'";
//			List<CreditsExchange> list = creditsExchangeManager.getHql(" from CreditsExchange where account='"+account+"'");
//			if(list!=null&&!list.isEmpty()&&list.get(0)!=null){
//			for (CreditsExchange mCreditsExchange : list) {
//				mCreditsExchange.setIsBlack(1);
//				creditsExchangeManager.update(mCreditsExchange);
//			}
//			}else{
//				ajaxPack("{state:false}", request, response);
//				return null;
//			}
////			if (creditsExchange.getState().equals("1")) {
////				List<User> userlist = userManager
////						.getHql(" from User where userid='"
////								+ creditsExchange.getUserid() + "'");
////				User user = userlist.get(0);
////				user.setHuIntegral(user.getHuIntegral()
////						- creditsExchange.getIntegral());
////				userManager.update(user);
////				creditsExchange.setState("3");
////				creditsExchange.setRemark(remark);
////				creditsExchange
////						.setOperTime(dateFm.format(new java.util.Date()));
////				creditsExchangeManager.update(creditsExchange);
////			}
//		} catch (Exception e) {
//			ajaxPack("{state:false}", request, response);
//			return null;
//		}
//		ajaxPack("{state:true}", request, response);
//
//		return null;
//	}
//	
//	/**
//	 * 删除黑名单
//	 **/
//	public ModelAndView updateIsBlacks(HttpServletRequest request,
//			HttpServletResponse response, CreditsExchange creditsExchange)
//			throws Exception {
//		try {
//			String account = request.getParameter("accounts");
//			String[] accounts = account.split(",,,,,,,,,,");
//			for (int i = 0; i < accounts.length; i++) {
//				List<CreditsExchange> list = creditsExchangeManager.getHql(" from CreditsExchange where account="+accounts[i]);
//				if(list!=null&&!list.isEmpty()&&list.get(0)!=null){
//				for (CreditsExchange mCreditsExchange : list) {
//					mCreditsExchange.setIsBlack(0);
//					creditsExchangeManager.update(mCreditsExchange);
//				}
//				}
//			}
//		} catch (Exception e) {
//			ajaxPack("{state:false}", request, response);
//			return null;
//		}
//		ajaxPack("{state:true}", request, response);
//
//		return null;
//	}

}
